﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Fin;
using ESOL.WinUI.MasterData;
using ESOL.Business.Mst;
using ESOL.WinUI.Pur;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainAPAccountDetail : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private MstSupplierEntity mSelectedSupplier;
        private FinSupplierAccountEntity mSelectedSupplierAccount;
        #endregion

        #region Contructor
        public frmMaintainAPAccountDetail()
        {
            InitializeComponent();
        }
        public frmMaintainAPAccountDetail(FinSupplierAccountEntity account)
        {
            InitializeComponent();
            mSelectedSupplierAccount = account;
            if (account != null)
            {
                mSelectedSupplier = MstSupplierBs.GetMstSupplierById(account.SupplierId);
                if (mSelectedSupplier != null)
                {
                    txtSupplier.Text = mSelectedSupplier.SupplierName;
                }
            }
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (mSelectedSupplier != null)
            {
                condition += " AND SAI.SUPPLIER_ID = '" + mSelectedSupplier.SupplierId + "' ";
            }
            if (txtAmountPerson.Tag != null && txtAmountPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtAmountPerson.Tag;
                condition += " AND SAI.AMOUNT_PERSON = '" + entity.PersonId + "' ";
            }
            if (dtpFromDate.EditValue!=null)
            {
                condition += " AND SAI.AMOUNT_DATE >= CONVERT(DATETIME,'" + dtpFromDate.EditValue.ToString() + "')";
            }
            if (dtpEndDate.EditValue != null)
            {
                condition += " AND SAI.AMOUNT_DATE <= CONVERT(DATETIME,'" + dtpEndDate.EditValue.ToString() + "')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = "";
            string orderBy = "SAI.AMOUNT_DATE DESC";
            DataTable dt = FinSupplierAccountItemBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindDataGrid();
            base.OnShown(e);
            dtpFromDate.Enabled = true;
            dtpEndDate.Enabled = true;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                mSelectedSupplier = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                mSelectedSupplier = null;
            }
        }

        private void txtAmountPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtAmountPerson.Text = frm.SelectedPerson.NameCn;
                txtAmountPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtAmountPerson.Text = "";
                txtAmountPerson.Tag = null;
            }
        }

        #endregion
    }
}
